/*
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package org.xptracker.test.services;

import org.xptracker.services.UpdateProvider;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class LocalFileUpdateProvider implements UpdateProvider {
  private static final String SETUP_BASE = "updates/org/xptracker/autoupdate/js/setup";
  private static final String TEST_BASE = "updates/org/xptracker/autoupdate/js/test";
  private static final FilenameFilter FILTER = new FilenameFilter() {
    public boolean accept(File file, String name) {
      return name.endsWith(".js");
    }
  };

  public Iterable<? extends String> listSetupFiles() {
    return makeStringList(new File(SETUP_BASE).listFiles(FILTER));
  }

  private Iterable<? extends String> makeStringList(File[] files) {
    List<String> results = new ArrayList<String>();
    if (files != null) {
      for (File file : files) {
        results.add(file.getName());
      }
    }
    return results;
  }

  public Reader loadSetupFile(String filename) throws FileNotFoundException {
    return new FileReader(new File(SETUP_BASE, filename));
  }

  public Iterable<? extends String> listUpdateFiles() {
    return makeStringList(new File(TEST_BASE).listFiles(FILTER));
  }

  public Reader loadUpdateFile(String filename) throws FileNotFoundException {
    return new FileReader(new File(TEST_BASE, filename));
  }
}
